﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Chat
{
    /// <summary>
    /// ChatWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ChatWindow : Window
    {
        public ChatWindow()
        {
            InitializeComponent();
            this.Title = string.Concat(Client.CurrentUser, " 正在聊天");
            this.Loaded += ChatWindow_Loaded;
        }

        bool hasSign = false;
        private async void ChatWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (!hasSign)
            {
                hasSign = true;
                await Client.CurrentService.SignIn(Client.CurrentUser.Name);
                List<ChatUser> users = await Client.CurrentService.GetOnlineUsers();
                this.listUser.ItemsSource = users;
            }

        }



        private async void WhenSendMessage(object sender, RoutedEventArgs e)
        {
            string message = txtContent.Text;
            if (string.IsNullOrEmpty(message))
            {
                txtStatus.Text = "请输入消息!";
                return;
            }
            await Client.CurrentService.Say(message);
        }

        int index = 0;

        private async void WhenContentKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                string message = txtContent.Text;
                if (string.IsNullOrEmpty(message))
                {
                    txtStatus.Text = "请输入消息!";
                    return;
                }
                await Client.CurrentService.SayTo(message, new ChatUser(Guid.NewGuid().ToString(), "Any")
                {
                    RuMing = (++index).ToString()
                });
            }
        }
    }
}
